package com.dotflat.milestone.service.impl;

import com.aspose.words.Document;
import com.dotflat.milestone.po.CheckSheet;
import com.dotflat.milestone.service.CheckSheetExportService;
import com.matt.Application;
import com.tuo51.foudation.utils.CopyUtils;
import freemarker.cache.ClassTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.TemplateNotFoundException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.Map;

import static freemarker.template.Configuration.VERSION_2_3_26;

@Service
public class CheckSheetExportServiceImpl implements CheckSheetExportService {
    //临时文件写出路径
    private static String tempPath = null;

    private static Configuration configuration = null;

    //初始化
    static {
        if(File.separatorChar == '/'){
            tempPath =  "/tmp/check_sheet";
        }else {
            tempPath =  "d:";
        }
    }
    //
    static {
        try {
            configuration = new Configuration(VERSION_2_3_26);
            configuration.setDefaultEncoding("utf-8");
            configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
            TemplateLoader tl = new ClassTemplateLoader(Application.class,"/");
            configuration.setTemplateLoader(tl);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public String exportEvaluateReport(CheckSheet checkSheet) {
            //create folder
            File tf = new File(tempPath);
            if(!tf.exists()){
                Boolean success = tf.mkdirs();
                if(success){
                    System.out.println("create file success::" + tempPath);
                }
            }

            String tempFilePath = tempPath + File.separatorChar + checkSheet.getUuid() +"_checkSheet.tmp";
            Writer out = null;
            try {
                //生成文档
                Template temple = null;
                String templateName = "freemarker/milestone_check_sheet_v1.xml";

                try {
                    temple = configuration.getTemplate(templateName);//获取模板
                }catch (TemplateNotFoundException e){
                    e.printStackTrace();
                }

                out = new OutputStreamWriter(new FileOutputStream(tempFilePath));//生成最终页面并写到文件

                Map data = CopyUtils.transBean2Map(checkSheet);

                temple.process(data, out);//处理

                String tt = convertXmlToDoc(tempFilePath);
                if (StringUtils.isNotEmpty(tt)){
                    tempFilePath = tt;
                }

                return tempFilePath;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    out.close();
                } catch (IOException e) {

                }
            }

            return  null;
        }

    private String convertXmlToDoc(String docFile) {

        try {
            // Open a document.
            Document doc = new Document(docFile);
            // Save document.
            String outputFileName = docFile + ".doc";

            doc.save(outputFileName);

            return outputFileName;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
